<?php

namespace Common\Model;

use Common\Model\AiModel;


class PhoneCodeModel extends AiModel
{
    
    //添加验证码
	//phone 手机号码
	//code 验证码
    //type 类型  1注册 2忘记密码 3手机号登录
    function addPhoneCode($phone,$code,$type){
       $addwhere['phone']=$phone;
       $addwhere['phonecode']=$code;
       $addwhere['ctime']=time();
       $addwhere['type']=$type;
       $this->add($addwhere);
    }
    //同一个手机时间段内允许最大注册发送次数
    //phone 手机号码
    //num   允许一天最大发送量
    //type  1注册 2忘记密码 3手机号登录 
    function sendCoadeMaxNum($phone,$num,$type){
    	$todaytime=strtotime(date('Y-m-d',time()));
    	$where['ctime']=array('gt',$todaytime);
    	$where['phone']=$phone;
    	$where['type'] =$type;
        $info=$this->where($where)->count();
        if($info>$num){//如果今天发送的条数大于规定的条数
          return '0';
        }else{
          return '1';
        }
    }

    //60秒内只能发送一个同类型的短信
    function sendCode60($phone,$type){
    	$time=time();
    	$where['phone']=$phone;
    	$where['type'] =$type;
        $info=$this->where($where)->find();
        $deleteTime=$time-$info['ctime'];
        if($deleteTime<=60){
            return '0';
        }else{
        	return '1';
        }
    }

    //验证短信是否正确
     function verPhoneCode($phone,$code,$type){
       $fwhere['phone']=$phone;
       $fwhere['type'] =$type;
       $fwhere['phonecode'] =$code;
       $fwhere['ctime'] =array('gt',strtotime(date('Y-m-d',time())));
       //验证此手机最后一个验证码
       $finfo=$this->where($fwhere)->order('id desc')->find();
       if($code==$finfo['phonecode']){
           return 1;
       }else{
           return 0;
       }
     }

}